package com.zmn.mcc.admin.controller.staff.beisen;

import com.zmn.common.dto.LoginStaff;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.request.RequestUtil;
import com.zmn.mcc.admin.controller.MccBaseController;
import com.zmn.mcc.business.interfaces.staff.ip.StaffIpInfoBService;
import com.zmn.mcc.common.constant.StaffChangeLogConsts;
import com.zmn.mcc.model.entity.log.IpInfo;
import com.zmn.mcc.model.entity.staff.McStaff;
import com.zmn.mcc.model.entity.staff.staffchangelog.StaffChangeLog;
import com.zmn.mcc.services.interfaces.staff.StaffService;
import com.zmn.mcc.services.interfaces.staff.staffchangelog.StaffChangeLogService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@RestController
@RequestMapping("/staff/beisen/")
public class BeisenController extends MccBaseController {
    @Resource
    private StaffService staffService;

    @Resource
    private StaffIpInfoBService staffIpInfoBService;

    @Resource
    private StaffChangeLogService staffChangeLogService;

    /**
     * 跳转绑定北森页面
     *
     * @param staffId 员工ID
     * @return
     */
    @RequestMapping(value = "toSetBeisenId")
    @ResponseBody
    public ModelAndView toSetBeisenId(@RequestParam(value = "staffId", defaultValue = "0") Integer staffId) {
        ModelAndView modelAndView = new ModelAndView("staff/setBeisenUserId");
        McStaff staff = staffId > 0 ? staffService.findByKey(staffId) : new McStaff();
        modelAndView.addObject("staff", staff);
        return modelAndView;
    }

    @RequestMapping(value = "synchro")
    @ResponseBody
    public ResponseDTO synchro(@RequestParam(value = "staffId", defaultValue = "0") Integer staffId,
                               @RequestParam(value = "bsUserId", defaultValue = "0") Integer bsUserId,
                               HttpServletRequest request) {
        if (NumberUtil.isNullOrZero(staffId)) {
            return ResponseDTO.fail(121701, "员工ID不能为空！");
        }
        if (NumberUtil.isNullOrZero(bsUserId)) {
            return ResponseDTO.fail(121701, "北森人员ID不能为空！");
        }

        // 更新员工北森用户ID
        int count = staffService.countByBsUserIdAndNotStaffId(staffId, bsUserId);
        if (count > 0) {
            return ResponseDTO.fail(121703, "该北森人员ID已被绑定，请核实后再试");
        }
        staffService.updateBsUserIdByStaffId(staffId, bsUserId, getStaffName());

        // 添加员工同步北森人员ID日志
        StaffChangeLog staffChangeLog = new StaffChangeLog();
        staffChangeLog.setStaffId(staffId);
        staffChangeLog.setBeforContent("-");
        staffChangeLog.setAfterContent("操作同步北森，北森人员ID：" + bsUserId);
        LoginStaff staffLogin = getLoginStaff();
        staffChangeLog.setAbutmenterId(staffLogin.getStaffId());
        staffChangeLog.setAbutmenter(staffLogin.getRealName());
        staffChangeLog.setAbutmenterDeptId(staffLogin.getDeptId() + "");
        staffChangeLog.setAbutmenterDeptName(staffLogin.getDeptName());
        staffChangeLog.setChangeType(StaffChangeLogConsts.CHANGE_TYPE_BAISEN);
        staffChangeLog.setCreateTime(DateUtil.getNow());
        String ip = RequestUtil.getRequestIp(request);
        IpInfo ipInfo = staffIpInfoBService.locationByIP(ip);
        if (Objects.nonNull(ipInfo)) {
            staffChangeLog.setAbutmenterCity(ipInfo.getProvince() + "-" + ipInfo.getCity());
        }
        staffChangeLog.setIp(ip);
        staffChangeLogService.insert(staffChangeLog);

        return ResponseDTO.success(null, "操作成功！");
    }
}
